using System;
using System.IO;
using System.Security.Policy;
using System.Xml;

namespace ImpEx.Engine
{
    public class FSDirectory : FSBase
    {
        public FSDirectory(WSSObject parentObj)
            : base(parentObj)
        {
            ChildrenCollection.Add(typeof(FSDirectory).Name, new WSSCollection());
            ChildrenCollection.Add(typeof(FSFile).Name, new WSSCollection());
        }

        public override void ChildrenDelayLoad(object o)
        {
            DirectoryInfo di = (DirectoryInfo)o;
            SPSProgressBar pb = new SPSProgressBar();
            pb.Start(ProgressBar);

            DirectoryInfo[] childDirs = di.GetDirectories();
            FileInfo[] childFiles = di.GetFiles();

            double range = 1d/(childDirs.Length + childFiles.Length);
            foreach(DirectoryInfo child in childDirs)
            {
                pb.NextRange(range);
                WSSObject fsd = WSSObjectFactory.Create(typeof(FSDirectory), this);
                fsd.LoadEntity(child);
                this.ChildrenCollection.AddEntity(fsd);
            }

            foreach(FileInfo child in childFiles)
            {
                pb.NextRange(range);
                WSSObject fsd = WSSObjectFactory.Create(typeof(FSFile), this);
                fsd.LoadEntity(child);
                this.ChildrenCollection.AddEntity(fsd);
            }
            pb.Completed();
        }

        public override void LoadEntity(object o)
        {
            DirectoryInfo di = (DirectoryInfo)o;
            Name = di.Name;
            LastModifiedDate = DateTime.MinValue;
            Path = di.FullName;
        }

        protected override void ChildrenDelayLoad()
        {
            ChildrenDelayLoad(new DirectoryInfo(Path));
        }

        public override void Export(WSSExport exportEngine)
        {
            //EnsureChildControls();
            base.Export(exportEngine);
        }
    }
}